﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Inventory
{
    /// <summary>
    /// Interaction logic for dlgProductGroups.xaml
    /// </summary>
    public partial class dlgProductGroups : Window
    {
        private CollectionViewSource _productGroupViewSource;// = new CollectionViewSource();
        private SalesManagerEntities _context;
        private int _ownerID;

        public dlgProductGroups(SalesManagerEntities context, int ownerID)
        {
            InitializeComponent();
            _context = context;
            _ownerID = ownerID;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _productGroupViewSource = (CollectionViewSource)FindResource("ProductGroups");
            _productGroupViewSource.Source = from c in _context.tblProductGroup
                                             where c.Owner.OwnerID == _ownerID
                                             orderby c.ShortName
                                             select c;
            _productGroupViewSource.View.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(View_CollectionChanged);
        }

        void View_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                foreach (tblProductGroup pg in e.NewItems)
                    pg.Owner = (tblOwner)_context.GetObjectByKey(new System.Data.EntityKey("SalesManagerEntities.tblOwner", "OwnerID", _ownerID));
            }
        }

        private void btnDone_Click(object sender, RoutedEventArgs e)
        {
            _context.SaveChanges();
            Close();
        }
    }
}
